/*
   Copyright 2011 James Bowling

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.armedia.dctm.constraints;

import com.documentum.fc.client.IDfType;
import com.documentum.fc.client.IDfTypedObject;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfAttr;
import com.documentum.fc.common.IDfId;
import com.documentum.fc.common.IDfTime;
import com.documentum.fc.common.IDfValue;

public class ConstraintUtils {
	
	public static String decodeAttributeDataType(IDfAttr attr) {
		return decodeAttributeDataType(attr.getDataType());
	}
	
	public static String decodeAttributeDataType(int dataTypeInt) {
		String dataType = "undefined";
		switch (dataTypeInt) {
		case IDfValue.DF_BOOLEAN:
			dataType = "boolean";
			break;
		case IDfValue.DF_DOUBLE:
			dataType = "double";
			break;
		case IDfValue.DF_ID:
			dataType = "ID";
			break;
		case IDfValue.DF_INTEGER:
			dataType = "integer";
			break;
		case IDfValue.DF_STRING:
			dataType = "string";
			break;
		case IDfValue.DF_TIME:
			dataType = "time";
			break;
		}
		return dataType;
	}	
	
	public static int getAttrIndex(final IDfType type, final String attributeName) throws DfException {
		// final int attrIndex = type.findAttrIndex(this.attributeName); <- this should work, but it doesn't 		
		int attrIndex = -1;
		if (type.getName().equals("dm_type")) {
			final int attrCount = type.getValueCount("attr_name");
			for(int i = 0; i < attrCount; i++) {
				final String name = type.getRepeatingString("attr_name", i);
				if (attributeName.equals(name)) {
					attrIndex = i;
					break;
				}
			}
		} else {
			for (int i = 0; i < type.getTypeAttrCount(); i++) {
				final String name = type.getTypeAttr(i).getName();
				if (name.equals(attributeName)) {
					attrIndex = i;
					break;
				}
			}			
		}
		return attrIndex;
	}
	
	public static boolean isNull(IDfTypedObject typedObject, IDfAttr attr, int index) throws DfException {
		boolean isNull = false;
		final int dataType = attr.getDataType();
		switch (dataType) {
		case IDfType.DF_STRING:
			final String s = typedObject.getRepeatingString(attr.getName(), index);
			isNull = s == null || s.length() == 0;
			break;				
		case IDfType.DF_ID:
			final IDfId id = typedObject.getRepeatingId(attr.getName(), index);
			isNull = id.isNull();
			break;
		case IDfType.DF_TIME:
			final IDfTime time = typedObject.getRepeatingTime(attr.getName(), index);
			isNull = time.isNullDate();
			break;
		case IDfType.DF_BOOLEAN:
		case IDfType.DF_DOUBLE:
		case IDfType.DF_INTEGER:
			break;	
		}		
		return isNull;
	}

	public static boolean isNull(IDfTypedObject typedObject, IDfAttr attr) throws DfException {
		boolean isNull = false;
		final int dataType = attr.getDataType();
		switch (dataType) {
		case IDfType.DF_STRING:
			final String s = typedObject.getString(attr.getName());
			isNull = s == null || s.length() == 0;
			break;				
		case IDfType.DF_ID:
			final IDfId id = typedObject.getId(attr.getName());
			isNull = id.isNull();
			break;
		case IDfType.DF_TIME:
			final IDfTime time = typedObject.getTime(attr.getName());
			isNull = time.isNullDate();
			break;
		case IDfType.DF_BOOLEAN:
		case IDfType.DF_DOUBLE:
		case IDfType.DF_INTEGER:
			break;
		default:
			throw new Error("Data type not handled in switch:" + dataType);
		}
		return isNull;
	}
	
	public static boolean isNullable(IDfAttr attr) {
		boolean isNullable = true;
		final int dataType = attr.getDataType();
		switch (dataType) {
		case IDfType.DF_BOOLEAN:
		case IDfType.DF_DOUBLE:
		case IDfType.DF_INTEGER:
			isNullable = false;
			break;
		}
		return isNullable;
	}

}
